#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import datetime
import requests
from fuc import *

user = { 'usr': 'wangsha', 'pwd': 'Wangsha!123' }
print("==> 当前用户", user['usr'])
driver = user_login(user)
print("==> 登陆结果")
cookie = cookie_check(driver)
# cookie='login_locale=zh_CN; avatarImageUrl=-790747946204903074; loginPageURL=; hostname=10.0.0.12:8080; JSESSIONID=9D5917D85084B6D9F5354E2ED7CDCD27'
print(cookie)

# print('==> 1.表单')
# print('http://172.25.22.209:8080/seeyon/cap4/businessTemplateController.do?method=capUnflowList&srcFrom=bizconfig&businessId=-1501808418815785942&moduleId=-1008028459191153221&formId=-3664222810819377396&type=baseInfo&tag=1638170030932&portalId=-2320350340341676070')

print('==> 2.筛选数据')
r=requests.post('http://172.25.22.209:8080/seeyon/rest/cap4/form/getCAPFormUnFlowList', headers={
  'Proxy-Connection': 'keep-alive',
  'Pragma': 'no-cache',
  'Cache-Control': 'no-cache',
  'Accept': 'application/json, text/plain, */*',
  'Origin': 'http://172.25.22.209:8080',
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
  'Content-Type': 'application/json;charset=UTF-8',
  'Referer': 'http://172.25.22.209:8080/seeyon/rest/cap4/unflow/-1501808418815785942/-1008028459191153221/1/index.html?bussId=-1501808418815785942&appId=-1008028459191153221&formId=-3664222810819377396&_t=1638263710949',
  'Accept-Encoding': 'gzip, deflate',
  'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7,zh-TW;q=0.6',
  'Cookie': cookie,
}, data='{"formId":"-3664222810819377396","formTemplateId":"-1008028459191153221","init":"0","userConditions":[{"aliasTableName":"formmain_0448","fieldName":"field0003","fieldValue":"2023-01-01","leftChar":"(","operation":"GreatEqual","rightChar":")","rowOperation":"and","fieldType":"TIMESTAMP","inputType":"date"}],"userOrderBy":[],"dataConditions":[],"bussId":"-1501808418815785942","appId":"-1008028459191153221","page":"1","pageSize":"50"}')
print(r)
# j=r.json()
# print(json.dumps(j))

# print('==> 3.点击导出')

# print('==> 3.1.验证按钮')
# print('http://172.25.22.209:8080/seeyon/rest/cap4/form/validBindButton')

# print('==> 3.2.获取表格列')
# print('http://172.25.22.209:8080/seeyon/rest/cap4/batch/unflow/excel/-3664222810819377396/-1008028459191153221')

print('==> 3.3.提交列，返回链接')
r=requests.post('http://172.25.22.209:8080/seeyon/rest/cap4/batch/unflow/excel/-3664222810819377396/-1008028459191153221', headers={
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7,zh-TW;q=0.6',
    'Cache-Control': 'no-cache',
    'Content-Length': '2172',
    'Content-Type': 'application/json;charset=UTF-8',
    'Cookie': cookie,
}, data='{"identifier":"[field0001]+[field0003]","fields":[{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0001","fieldDisplay":"姓名"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0002","fieldDisplay":"厂商"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0003","fieldDisplay":"考勤日期"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0010","fieldDisplay":"日期类型"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0004","fieldDisplay":"上班打卡时间"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0008","fieldDisplay":"上班考勤状态"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0013","fieldDisplay":"上班异常原因"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0005","fieldDisplay":"下班打卡时间"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0009","fieldDisplay":"下班考勤状态"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0014","fieldDisplay":"下班异常原因"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0015","fieldDisplay":"上班时长"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0025","fieldDisplay":"加班时长"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0007","fieldDisplay":"请假天数"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"field0038","fieldDisplay":"项目名称"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"start_member_id","fieldDisplay":"创建人"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"start_date","fieldDisplay":"创建时间"},{"tableName":"formmain_0448","tableDisplay":"主表","master":true,"fieldName":"modify_date","fieldDisplay":"修改时间"}],"queryParams":{}}'.encode("UTF-8"))
print(r)
j=r.json()
# print(json.dumps(j))
x='http://172.25.22.209:8080' + j['data']['data']['href']
print(x)

print('==> 4.下载文件')
time.sleep(2)
r=requests.get(x, allow_redirects=True, headers={
    'Proxy-Connection': 'keep-alive',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    'Referer': 'http://172.25.22.209:8080/seeyon/cap4/businessTemplateController.do?method=capUnflowList&srcFrom=bizconfig&businessId=-1501808418815785942&moduleId=-1008028459191153221&formId=-3664222810819377396&type=baseInfo&tag=1638170030932&portalId=-2320350340341676070',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7,zh-TW;q=0.6',
    'Cookie': cookie,
})
print(r)
n=datetime.datetime.now().strftime("%Y%m%d") + '.xlsx'
with open(n, 'wb') as f:
    f.write(r.content)
print(n)
